package com.bicon.botu.mall.order.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.mall.order.common.OrderConstans;
import com.bicon.botu.mall.order.entity.OrderGoods;
import com.bicon.botu.mall.order.entity.OrderReceipt;
import com.bicon.botu.mall.order.service.IOrderChildService;
import com.bicon.botu.mall.order.service.IOrderReceiptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.lang.invoke.ConstantCallSite;
import java.util.Optional;

/**
 * <p>
 * <p>
 * 订单支付
 * </p>
 *
 * @author tao.zeng.
 * @since 2018/1/16.
 */
@Slf4j
@Component
public class OrderKafkaListener {


    @Autowired
    private IOrderReceiptService iOrderReceiptService;

    @Autowired
    private IOrderChildService iOrderChildService;


    /**
     * 支付
     *
     * @param record
     */
    @KafkaListener(topics = {OrderConstans.TOPIC_BOTU_MALL_ORDER})
    public void listen(ConsumerRecord<?, ?> record) {
        Optional<?> kafkaMessage = Optional.ofNullable(record.value());
        if (kafkaMessage.isPresent()) {
            Object message = kafkaMessage.get();
            log.info("kafka返回结果Order：" + message);
            JSONObject jsonObject = JSON.parseObject(message.toString());
            String result = jsonObject.getString("result");
            String orderNo = jsonObject.getString("orderNo");
            OrderReceipt orderReceipt = iOrderReceiptService.selectOne(new EntityWrapper<OrderReceipt>().eq("transaction_no", orderNo));
            if (null != orderReceipt) {
                if ("fail".equals(result)) {
                    orderReceipt.setPayState(Integer.valueOf(OrderConstans.WALLET_STATE_FAIL));
                    OrderGoods orderGoods = new OrderGoods().selectOne(new EntityWrapper<OrderGoods>().eq("order_receipt_id",orderReceipt.getId()).eq("logic_delete",Constants.DEL_LOGIC_NORMAL));
                    orderGoods.setPayState(1);
                    orderGoods.preUpdate();
                    orderGoods.updateById();
                } else {
                    orderReceipt.setPayState(Integer.valueOf(OrderConstans.WALLET_STATE_SUCCESS));
                    OrderGoods orderGoods = new OrderGoods().selectOne(new EntityWrapper<OrderGoods>().eq("order_receipt_id",orderReceipt.getId()).eq("logic_delete",Constants.DEL_LOGIC_NORMAL));
                    orderGoods.setPayState(2);
                    orderGoods.preUpdate();
                    orderGoods.updateById();
                    iOrderChildService.addSales(orderGoods.getId());
                }
                orderReceipt.preUpdate();
                orderReceipt.updateById();
            }
        }
    }
}
